package com.wanmait.wisdom.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.wisdom.config.RedisConfig;
import com.wanmait.wisdom.entity.Resident;
import com.wanmait.wisdom.mapper.OwnerMapper;
import com.wanmait.wisdom.mapper.ResidentMapper;
import com.wanmait.wisdom.util.AjaxResult;
import com.wanmait.wisdom.util.SMSUtils;
import com.wanmait.wisdom.web.service.WebResidentService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

@Service
@Transactional(rollbackFor = Exception.class)
public class WebResidentServiceImpl extends ServiceImpl<ResidentMapper,Resident> implements WebResidentService {
    // 实现业务逻辑

    @Resource
    private ResidentMapper residentMapper;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public Resident login(Resident resident) {
        LambdaQueryWrapper<Resident> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(resident.getPhone1()!=null,Resident::getPhone1,resident.getPhone1());

        queryWrapper.eq(resident.getEmail()!=null,Resident::getEmail,resident.getEmail());
        queryWrapper.eq(Resident::getPassword,resident.getPassword());
        Resident loginResident =  residentMapper.selectOne(queryWrapper);
        if (loginResident==null){
            loginResident = loginWithOtherPhone(resident);
        }
        //redisTemplate.opsForValue().set("resident",loginResident.toString());
        return loginResident;

    }
    @Override
    public Resident loginWithOtherPhone(Resident resident) {
        LambdaQueryWrapper<Resident> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(resident.getPhone1()!=null,Resident::getPhone2,resident.getPhone1());
        queryWrapper.eq(resident.getEmail()!=null,Resident::getEmail,resident.getEmail());
        queryWrapper.eq(Resident::getPassword,resident.getPassword());
        Resident loginResident =  residentMapper.selectOne(queryWrapper);
        //redisTemplate.opsForValue().set("resident",loginResident.toString());
        return loginResident;
    }
    //短信验证码
    @Override
    public void sendCode(String phone, String code) {
        SMSUtils.sendMessage("阿里云短信测试","SMS_154950909",phone ,code);
    }

    @Override
    public Resident getResidentByPhone1(String phone) {
        LambdaQueryWrapper<Resident> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(phone!=null,Resident::getPhone1,phone);
        Resident loginResident =  residentMapper.selectOne(queryWrapper);
        if (loginResident==null){
            loginResident = getResidentByPhone2(phone);
        }
        return loginResident;
    }
    @Override
    public Resident getResidentByPhone2(String phone) {
        LambdaQueryWrapper<Resident> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(phone!=null,Resident::getPhone1,phone);
        Resident loginResident =  residentMapper.selectOne(queryWrapper);
        return loginResident;
    }
    @Override
    public Resident findById(Integer id) {
        return residentMapper.selectById(id);
    }

    //可修改的信息
    @Override
    public void updateEmail(Integer id, String email) {
        residentMapper.updateEmail(id,email);
    }

    @Override
    public void updatePhone1(Integer id, String phone1) {
        residentMapper.updatePhone1(id,phone1);
    }

    @Override
    public void updatePhone2(Integer id, String phone2) {
        residentMapper.updatePhone2(id,phone2);
    }

    @Override
    public void sendCode2(String phone, String s) {
        SMSUtils.sendMessage("阿里云短信测试","SMS_154950909",phone ,s);
    }

    @Override
    public Integer getCountByPhone(String phone) {
        Integer count =residentMapper.getCountByPhone(phone);

        return count;
    }


}
